<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * @version $Id: TeamTool.class.php 1126 2008-06-09 05:45:04Z julienv $
 * @package Joomleague
 * @copyright Copyright (C) 2007  JoomLeague
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see _joomleague_license.txt
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/ 

class TeamTool extends JLClass{
  
  var $id;
  var $project_id;
  var $team_id;
  var $division_id;
  var $standard_playground;
  var $start_points;
  var $admin;
  var $info;
  var $checked_out;
  var $checked_out_time;
  var $picture;
  var $description;
  
  function TeamTool( &$db ) {
		$this->mosDBTable( '#__joomleague_team_joomleague', 'id', $db );
	}

	
  function assignStaff($post = null){
    
    if(isset($post["staffmembers"])){
      
      $query = "SELECT p.id 
                FROM #__joomleague_players p, #__joomleague_teamstaff_project ts
                WHERE p.id = ts.person_id AND ts.projectteam_id = $this->id";
              
      $this->_db->setQuery($query);
      
      $old_list = $this->_db->loadResultArray(); //old
      $new_list = explode(",",$post["staffmembers"]); //new
      
      print_r($old_list);
      print "<br/>";
      print_r($new_list);
      $remove = array_diff( $old_list, $new_list ); // remove these Items
      $insert = array_diff( $new_list, $old_list ); // insert these Items
      
      print_r($remove);
      print "<br/>";
      print_r($insert);
      
      #remove
      if(count($remove) > 0){                
        $list = implode(",",$remove);
        $this->_db->setQuery("DELETE FROM #__joomleague_teamstaff_project 
                              WHERE projectteam_id = $this->id AND person_id in ($list)");
        $this->_db->query();
        print $this->_db->getQuery();
        
      }
      #insert
      if(count($insert) > 0){
        $query = "";
        foreach($insert as $v){
            $query .= "INSERT INTO #__joomleague_teamstaff_project (projectteam_id, person_id) values ($this->id,$v);\n";
        }
        
        $this->_db->setQuery($query);
        print $this->_db->getQuery();
        $this->_db->query_batch(false);
        
        //print mysql_error(); 
      }
    }
  }
  
}

?>
